package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 护理等级分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> getList(Integer pageNum, Integer pageSize, String name, Integer status) {
        if (pageNum == null || pageSize == null) {
             throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //设置起始页码
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingLevelVo> page = nursingLevelMapper.selectList(name,status);
        //
        return PageResponse.of(page, NursingLevelVo.class);
    }

    /**
     * 新增护理等级
     * @param levelDto
     */
    @Override
    public void addNursingLevel(NursingLevelDto levelDto) {
        if (levelDto == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        NursingLevel nursingLevel = BeanUtil.toBean(levelDto, NursingLevel.class);
        nursingLevelMapper.insertNursingLevel(nursingLevel);
    }

    /**
     * 根据id查询护理等级
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getById(Long id) {
        if (id == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //先查询护理等级信息
        NursingLevel nursingLevel = nursingLevelMapper.selectById(id);
        //再根据计划Id查询关联的护理计划
        String plans = nursingPlanMapper.selectPlans(nursingLevel.getPlanId());
        NursingLevelVo nursingLevelVo = BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
        nursingLevelVo.setPlanName(plans);
        return nursingLevelVo;
    }

    /**
     * 更新护理等级
     * @param levelDto
     */
    @Override
    public void updateNursingLevel(NursingLevelDto levelDto) {
        if (levelDto == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        NursingLevel nursingLevel = BeanUtil.toBean(levelDto, NursingLevel.class);
        nursingLevelMapper.updateNursingLevel(nursingLevel);
    }

    /**
     * 删除护理等级
     * @param id
     */
    @Override
    public void deleteNursingLevel(Long id) {
        if (id == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        nursingLevelMapper.deleteNursingLevel(id);
    }

    /**
     * 更新启停状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        if (id == null || status == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //如果启用护理等级时，关联的护理计划未被启用时，则不能停用该等级
        if (status == 1) { //表示要更改未启用计划
            //先查询该等级下的护理计划
            NursingLevel nursingLevel = nursingLevelMapper.selectById(id);
            //再根据计划id查询该计划启停状态
            NursingPlanVo planVo = nursingPlanMapper.getById(nursingLevel.getPlanId());
            if (planVo.getStatus() == 0) {
                throw new BaseException(BasicEnum.NURSING_LEVEL_FAILED);
            }
        }
        NursingLevel nursingLevel = new NursingLevel();
        nursingLevel.setStatus(status);
        nursingLevel.setId(id);
        nursingLevelMapper.updateNursingLevel(nursingLevel);
    }
}
